package rach.grid.andgrid1;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	//private Bitmap bitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//if (bitmap == null) {
		
		WorldParameters wp = new WorldParameters(1, 153, 1, 88, -4,4);
		int panelWidth = (wp.getMaxGridX() - wp.getMinGridX())+1;
		int panelHeight = (wp.getMaxGridY() - wp.getMinGridY()) + 1;
		
		
		
		InitialiseGrid ig =null;
		try {
			ig = new InitialiseGrid(wp, new AndroidReadFile(this).getDirectoryFiles(),false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		WorldGrid grid = ig.getGrid();
		Map<Integer, Map<Integer, GridCell>> yMap = grid.getzMap().get(0);
		MakePixelArray pixArray = new MakePixelArray(wp, yMap);
		
		ImageView iv = new ImageView(this);
		int[] pixList = pixArray.getPixelList();
		System.out.println(pixList.length);

		Bitmap bitmap = Bitmap.createBitmap(panelWidth, panelHeight, Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixList, 0, panelWidth, 0, 0, panelWidth, panelHeight);
		
	    iv.setDrawingCacheEnabled(true);
	    iv.setAdjustViewBounds(true);
		iv.setVisibility(View.VISIBLE);
		iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		iv.setImageBitmap(Bitmap.createScaledBitmap(bitmap, panelWidth*10, panelHeight*10, false));

		LayoutParams lp = new LayoutParams(panelHeight*10, panelHeight*10);
		this.addContentView(iv, lp);

		//}
	}
	
//	public Object onRetainNonConfigurationInstance() {
//	    final MyDataObject data = collectMyLoadedData();
//	    return data;
//	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
